package com.gengzp.rpc.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.gengzp.rpc.model.requestIdentification.RpcRequestIdentification;

/**
 * @ClassName RpcRequestContextHolder
 * @Description rpc框架请求上下文线程变量
 * <p>使用阿里提供的TransmittableThreadLocal, 确保在多线程的情况下线程变量不会丢失</p>
 * @Author gengzp
 * @Date 2025/8/22 11:33
 */
public class RpcRequestContextHolder {

    /**
     * 请求标识信息
     */
    private static final ThreadLocal<RpcRequestIdentification> requestIdentification = new TransmittableThreadLocal<>();

    /**
     * 获取请求标识信息
     *
     * @return 请求标识信息
     */
    public static RpcRequestIdentification getRequestIdentification() {
        return requestIdentification.get();
    }

    /**
     * 保存请求标识信息
     *
     * @param requestIdentification 保存请求标识信息
     */
    public static void setRequestIdentification(RpcRequestIdentification requestIdentification) {
        RpcRequestContextHolder.requestIdentification.set(requestIdentification);
    }

    /**
     * 清除请求标识信息
     */
    public static void clearRequestIdentification() {
        requestIdentification.remove();
    }

}
